package com.fly.web;

import com.fly.base.Result;
import com.fly.base.ResultGenerator;
import com.fly.domain.model.Customer;
import com.fly.domain.model.CustomerCashInfo;
import com.fly.domain.model.CustomerFeedbackInfo;
import com.fly.domain.model.CustomerOfferInfo;
import com.fly.domain.service.CustomerService;
import com.fly.service.MyCustomerService;
import com.fly.util.SidService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.HashMap;
import java.util.Map;

import static com.fly.util.SidService.Biz.CUSTOMER;

/**
 * @author frinder
 * @date 2018/5/19
 * @className MyCustomerController
 * @package com.fly.web
 * @Description: ${todo}
 */
@RestController
@RequestMapping("/myCustomer")
public class MyCustomerController {

    @Resource
    private CustomerService customerService;

    @Autowired
    private MyCustomerService myCustomerService;

    @Autowired
    private SidService sidService;

    @PostMapping("/add")
    public Result add(Customer customer) {
        customer.setSid(sidService.getSid(CUSTOMER));
        customerService.save(customer);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "获取顾客信息")
    @GetMapping("/sid")
    public Result getCustomer(@RequestParam String customerSid) {
        Customer customer = customerService.findBy("sid", customerSid);
        return ResultGenerator.genSuccessResult(customer);
    }

    @ApiOperation(value = "获取顾客最后一次邀约/反馈信息")
    @GetMapping("/getLastInfo")
    public Result getLastInfo(@RequestParam String customerSid) {
        Customer customer = customerService.findBy("sid", customerSid);
        CustomerOfferInfo offerInfo = myCustomerService.getLastOfferInfo(customerSid);
        CustomerFeedbackInfo feedbackInfo = myCustomerService.getLastFeedbackInfo(customerSid);
        CustomerCashInfo cashInfo = myCustomerService.getLastCashInfo(customerSid);
        Map<String, Object> result = new HashMap(4) {{
            put("customer", customer);
            put("offer", offerInfo);
            put("feedback", feedbackInfo);
            put("cash", cashInfo);
        }};
        return ResultGenerator.genSuccessResult(result);
    }

}
